import { defineStore } from "pinia";
import { getOverviewData, getCaseRanking, type OverviewData, type CaseRanking } from "@/api/overview";
import { ref } from "vue";

export const useOverviewStore = defineStore("overview", () => {
	// 首页概览数据
	const overviewData = ref<OverviewData | null>(null);
	// 案例排行榜数据
	const caseRanking = ref<CaseRanking[]>([]);
	// 加载状态
	const loading = ref(false);
	// 排行榜加载状态
	const rankingLoading = ref(false);

	// 获取首页概览数据
	const fetchOverviewData = async () => {
		try {
			loading.value = true;
			const res = await getOverviewData();
			if (res.code === 200) {
				overviewData.value = res.data;
			}
		} catch (error) {
			console.error("获取首页概览数据失败", error);
		} finally {
			loading.value = false;
		}
	};

	// 获取案例排行榜数据
	const fetchCaseRanking = async () => {
		try {
			rankingLoading.value = true;
			const res = await getCaseRanking();
			if (res.code === 200) {
				caseRanking.value = res.data;
			}
		} catch (error) {
			console.error("获取案例排行榜数据失败", error);
		} finally {
			rankingLoading.value = false;
		}
	};

	return {
		overviewData,
		caseRanking,
		loading,
		rankingLoading,
		fetchOverviewData,
		fetchCaseRanking
	};
}); 